package com.haisen.service.impl;

import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.InsertBatch;
import com.haisen.core.utils.MyMapper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.BussItemT;
import com.haisen.dataobject.entity.Customer;
import com.haisen.dataobject.mapper.base.BussItemTMapper;
import com.haisen.dataobject.query.BussItemTQuery;
import com.haisen.poi.basic_poi.BigExcelUtil;
import com.haisen.service.BussItemTService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Data
public class BussItemTServiceImpl implements BussItemTService {


    @Autowired
    private BussItemTMapper bussItemTMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    private InsertBatch<BussItemT> insertBatch = null;

    @Override
    public void create(BussItemT bussItemT) {
        bussItemTMapper.insertSelective(bussItemT);
    }

    @Override
    public void update(BussItemT bussItemT) {
       bussItemTMapper.updateByPrimaryKeySelective(bussItemT);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<BussItemT> example = Weekend.of(BussItemT.class);
        WeekendCriteria<BussItemT,Object> criteria = example.weekendCriteria();
        criteria.andIn(BussItemT::getId, Arrays.asList(ids));
        bussItemTMapper.deleteByExample(example);
    }

    @Override
    public void createbatch(List<BussItemT> bussItemTList) {

        SqlSession sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH);
        MyMapper<BussItemT> myMapper = (MyMapper<BussItemT>) sqlSession.getMapper(BussItemTMapper.class);
        insertBatch = new InsertBatch<>();
        insertBatch.insertBatch(sqlSession,myMapper,bussItemTList);
    }

    @Override
    public PageResultSet<BussItemT> findByPage(BussItemTQuery bussItemTQuery) {
        //分页参数设置
        if (!StringUtils.isEmpty(bussItemTQuery.getOrderBy())){
            PageHelper.orderBy(bussItemTQuery.getOrderBy());
        }
        PageHelper.offsetPage(bussItemTQuery.getOffset(),bussItemTQuery.getLimit());

        Weekend<BussItemT> example = Weekend.of(BussItemT.class);
        WeekendCriteria<BussItemT,Object> criteria = example.weekendCriteria();
        if (!StringUtils.isEmpty(bussItemTQuery.getPhone())){
            criteria.andLike(BussItemT::getPhone,bussItemTQuery.getPhone());
        }
        if (!StringUtils.isEmpty(bussItemTQuery.getHandleDate())){
            criteria.andEqualTo(BussItemT::getBizType,bussItemTQuery.getBizType());
        }
        if (!StringUtils.isEmpty(bussItemTQuery.getHandleNo())){
            criteria.andEqualTo(BussItemT::getHandleNo,bussItemTQuery.getHandleNo());
        }
         List<BussItemT> bussItemTList = bussItemTMapper.selectByExample(example);
        long total = bussItemTMapper.selectCountByExample(example);
        PageResultSet<BussItemT> resultSet = new PageResultSet<>();
        resultSet.setRows(bussItemTList);
        resultSet.setTotal(total);
        return resultSet;
    }


    @Override
    public void importExcel2DataBase(String pathFile) {
        BigExcelUtil bigExcelUtil = BigExcelUtil.getInstance();
        List<Object> objs =  bigExcelUtil.importExcel2Obj(pathFile,BussItemT.class);
        List<BussItemT> bussItemTList = objs.stream().map( o ->{
            return (BussItemT)o;
        }).collect(Collectors.toList());
        createbatch(bussItemTList);
    }

}
